package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.macro.mall.dto.e.DataAnalysisPictureEntDTO;
import com.macro.mall.dto.o.DataAnalysisPictureOutDTO;
import com.macro.mall.mapper.BasicKeyImgMapper;
import com.macro.mall.mapper.DpsTagImgMapper;
import com.macro.mall.model.BasicKeyImg;
import com.macro.mall.model.DpsTagImg;
import com.macro.mall.service.DataAnalysisPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DataAnalysisPictureServiceImpl implements DataAnalysisPictureService {

    @Autowired
    private DpsTagImgMapper dpsTagImgMapper;
    @Autowired
    private BasicKeyImgMapper basicKeyImgMapper;

    @Override
    public PageInfo<DataAnalysisPictureOutDTO> list(DataAnalysisPictureEntDTO dataAnalysisPictureEntDTO) {
        PageHelper.startPage(dataAnalysisPictureEntDTO.getPage(),dataAnalysisPictureEntDTO.getPageSize());
        List<DpsTagImg> list = dpsTagImgMapper.list(dataAnalysisPictureEntDTO.getLabel());
        PageInfo<DpsTagImg> dpsTagImgPageInfo = new PageInfo<>(list);
        List<DataAnalysisPictureOutDTO> list1=new ArrayList<>();
        for (DpsTagImg dpsTagImg:list){
            DataAnalysisPictureOutDTO dataAnalysisPictureOutDTO = new DataAnalysisPictureOutDTO();
            dataAnalysisPictureOutDTO.setId(dpsTagImg.getId());
            dataAnalysisPictureOutDTO.setImgName(dpsTagImg.getImgName());
            dataAnalysisPictureOutDTO.setImgPath(dpsTagImg.getImgPath());
            dataAnalysisPictureOutDTO.setCreatedDt(dpsTagImg.getCreatedDt());
            dataAnalysisPictureOutDTO.setStatus(dpsTagImg.getStatus());
            dataAnalysisPictureOutDTO.setLabel(dpsTagImg.getKeyType());
            list1.add(dataAnalysisPictureOutDTO);
        }
        PageInfo<DataAnalysisPictureOutDTO> dataAnalysisPictureOutDTOPageInfo = new PageInfo<>(list1);
        dataAnalysisPictureOutDTOPageInfo.setTotal(dpsTagImgPageInfo.getTotal());
        return dataAnalysisPictureOutDTOPageInfo;
    }

    @Override
    public int modify(DataAnalysisPictureEntDTO dataAnalysisPictureEntDTO) {
        if (dataAnalysisPictureEntDTO.getStatus() !=null && dataAnalysisPictureEntDTO.getStatus() !=""){
            DpsTagImg dpsTagImg = new DpsTagImg();
            dpsTagImg.setId(dataAnalysisPictureEntDTO.getId());
            dpsTagImg.setStatus(dataAnalysisPictureEntDTO.getStatus());
            return dpsTagImgMapper.updateByPrimaryKeySelective(dpsTagImg);

        }
        if (dataAnalysisPictureEntDTO.getImgName() !=null && dataAnalysisPictureEntDTO.getImgName() !=""){
            BasicKeyImg basicKeyImg = basicKeyImgMapper.selectByPrimaryKey(dataAnalysisPictureEntDTO.getId());
            BasicKeyImg basicKeyImg1 = new BasicKeyImg();
            basicKeyImg1.setId(basicKeyImg.getId());
            basicKeyImg1.setImgName(dataAnalysisPictureEntDTO.getImgName());
            basicKeyImg1.setImgPath(dataAnalysisPictureEntDTO.getImgPath());
            basicKeyImgMapper.updateByPrimaryKeySelective(basicKeyImg1);
        }
        if (dataAnalysisPictureEntDTO.getLabel() !=null && dataAnalysisPictureEntDTO.getLabel() !=""){
            DpsTagImg dpsTagImg = new DpsTagImg();
            dpsTagImg.setId(dataAnalysisPictureEntDTO.getId());
            dpsTagImg.setKeyType(dataAnalysisPictureEntDTO.getLabel());
            return dpsTagImgMapper.updateByPrimaryKeySelective(dpsTagImg);

        }
        return 0;

    }
}
